package pers.xhh.frontapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.xhh.frontapi.annotation.UserLoginToken;
import pers.xhh.frontapi.common.Mi;
import pers.xhh.frontapi.entity.to.cart.CartTo;
import pers.xhh.frontapi.service.CartService;

/**
 * @author wei-xhh
 * @date 2020-12-08
 */
@CrossOrigin
@RestController
@RequestMapping("/mi/api")
public class CartController {

    @Autowired
    CartService cartService;

    @UserLoginToken
    @PostMapping("/cart/add")
    public Mi addCart(@RequestBody CartTo cartTo) throws JsonProcessingException {
        return cartService.addToCart(cartTo);
    }

    @UserLoginToken
    @GetMapping("/cart/{goodsId}")
    public Mi getCartItem(@PathVariable("goodsId") Integer goodsId) throws JsonProcessingException {
        return cartService.getCartItem(goodsId);
    }

    @UserLoginToken
    @GetMapping("/cart/list")
    public Mi getCartList(){
        return cartService.getCartList();
    }

    @UserLoginToken
    @DeleteMapping("/cart/clear")
    public Mi clearCart(){
        return cartService.clearCart();
    }

    @UserLoginToken
    @PostMapping("/cart/select")
    public Mi selectItem(@RequestParam("goodsId") Integer goodsId,
                         @RequestParam("check") Integer check) throws JsonProcessingException {
        return cartService.selectItem(goodsId, check);
    }

    @UserLoginToken
    @PostMapping("/cart/add/count")
    public Mi addCartCount(@RequestParam("goodsId") Integer goodsId,
                      @RequestParam("num") Integer num) throws JsonProcessingException {
        return cartService.addCartCount(goodsId, num);
    }

    @UserLoginToken
    @DeleteMapping("/cart/{goodsId}")
    public Mi deleteCartItem(@PathVariable("goodsId") Integer goodsId){
        return cartService.deleteCartItem(goodsId);
    }

    @UserLoginToken
    @PostMapping("/cart/select/all")
    public Mi selectAll(){
        return cartService.selectAll();
    }

    @UserLoginToken
    @PostMapping("/cart/done/all")
    public Mi doneAll(){
        return cartService.doneAll();
    }
}
